/* DxfFile.java
 *
 * Created on 4 de Dezembro de 2007, 10:02
 *
 * Modified on 08/19/2011, 11:04 PM by Adriano
 * Included the attribute @Override to inherited methods
 */

package importadorDXF;

import java.io.InputStream;
import java.util.Hashtable;
import utility.I18n;

/**
 *
 * @author rafaelbuck
 */
public class DxfFile implements DxfReaderReporter
{    
    // Classe Interna
    static class Counter
    {
        private int count;
        
        Counter()
        {
        }
        
        public void increase()
        {
            count++;
        }

        public int getCount()
        {
            return count;
        }

        @Override
        public String toString()
        {
            return Integer.toString(count);
        }        
    }

    /** Creates a new instance of DxfFile */
    // Esse metodo eh utilizado pela classe principal ReaderTester
    public DxfFile(String path, DxfReporter report) throws DxfException
    {
        countHash = new Hashtable();
        reporter = report;
        grp = new DxfGroups(path, this, this);
        read();
    }

    /** Creates a new instance of DxfFile */
    public DxfFile(String path) throws DxfException
    {
        this(path, null);
    }

    /** Creates a new instance of DxfFile */
    public DxfFile(InputStream is, DxfReporter report) throws DxfException
    {
        countHash = new Hashtable();
        reporter = report;
        grp = new DxfGroups(is, this, this);
        read();
    }

    /** Creates a new instance of DxfFile */
    public DxfFile(InputStream is) throws DxfException
    {
        this(is, null);
    }

    // le o arquivo DXF
    private void read() throws DxfException
    {
        String code;
label0:
        do
            do
            {
                grp.read();
                //String code;
                if(grp.number == 0)
                {
                    if(!(code = grp.valToString()).equals("SECTION"))
                        continue label0;
                    
                    grp.read();
                    
                    if(grp.number != 2)
                        throw new DxfException("err!FpFormat", new String[] {
                            String.valueOf(grp.getLineNr())
                        });
                    
                    code = grp.valToString();
                    
                    if(code.equals("HEADER"))
                    {
                        showStatusRes("msgReading", new String[] {
                            "HEADER"
                        });
                        header = new DxfHeader();
                        header.read(grp, this);
                    } else
                    if(code.equals("TABLES"))
                    {
                        showStatusRes("msgReading", new String[] {
                            "TABLES"
                        });
                        tables = new DxfTables();
                        tables.read(grp, this);
                    } else
                    if(code.equals("CLASSES"))
                    {
                        showStatusRes("msgReading", new String[] {
                            "CLASSES"
                        });
                        (new DxfSection(false)).read(grp, this);
                    } else
                    if(code.equals("OBJECTS"))
                    {
                        showStatusRes("msgReading", new String[] {
                            "OBJECTS"
                        });
                        (new DxfSection(false)).read(grp, this);
                    } else
                    if(code.equals("BLOCKS"))
                    {
                        showStatusRes("msgReading", new String[] {
                            "BLOCKS"
                        });
                        blocks = new DxfBlocks();
                        blocks.read(grp, this);
                    } else
                    if(code.equals("ENTITIES"))
                    {
                        showStatusRes("msgReading", new String[] {
                            "ENTITIES"
                        });
                        entities = new DxfEntities();
                        entities.read(grp, this);
                    } else
                    {
                        DxfSection foo = new DxfSection();
                        foo.read(grp, this);
                    }
                } else
                {
                    throw new DxfException("err!Syntax", new String[] {
                        String.valueOf(grp.getLineNr())
                    });
                }
            } while(true);
        while(!code.equals("EOF"));
        
        showStatusRes("msgEOF");
        
        if(getLayer("0", false) == null)
            getTables().createLayer0(this);
        
        if(getLtype("CONTINUOUS") == null)
            getTables().createLtypeContinuous(this);
        
        if(getStyle("STANDARD") == null)
            getTables().createStyleStd(this);
        
        if(header == null)
        {
            showWarningRes("warnNoHeader");
            header = new DxfHeader();
        } 
        else if(header.getACADVER() == null)
            showWarningRes("warnNoAcadver");
    }

    @Override
    public void showStatus(String msg)
    {
        if(reporter != null)
            reporter.showStatus(msg);            
    }

    @Override
    public void showStatusRes(String tag)
    {
        if(reporter != null)
            reporter.showStatusRes(tag);
    }

    @Override
    public void showStatusRes(String tag, String args[])
    {
        if(reporter != null)
            reporter.showStatusRes(tag, args);
    }

    @Override
    public void showWarning(String msg)
    {
        if(reporter != null)
            reporter.showWarning(msg);
    }

    @Override
    public void showWarningRes(String tag)
    {
        if(reporter != null)
            reporter.showWarningRes(tag);
    }

    @Override
    public void showWarningRes(String tag, String args[])
    {
        if(reporter != null)
            reporter.showWarningRes(tag, args);
    }

    @Override
    public void countEntity(String name)
    {
        Counter nr = (Counter)countHash.get(name);
        
        if(nr != null)
        {
            nr.increase();
        } else
        {
            nr = new Counter();
            nr.increase();
            countHash.put(name, nr);
        }
    }

    @Override
    public void warnUnknownEntity(String name)
    {
        Counter nr = (Counter)countHash.get(name);
        
        if(nr != null)
        {
            nr.increase();
        } else
        {
            nr = new Counter();
            nr.increase();
            countHash.put(name, nr);
            showWarningRes("warnUnknownEntity", new String[] {
                name
            });
        }
    }

    @Override
    public void showProgress()
    {
        if(reporter != null)
            reporter.showProgress();
    }

    @Override
    public void showProgress(float level)
    {
        if(reporter != null)
            reporter.showProgress(level);
    }

    @Override
    public void showProgress(float level, float maxLevel)
    {
        if(reporter != null)
            reporter.showProgress(level, maxLevel);
    }

    @Override
    public void showProgress(long level, long maxLevel)
    {
        if(reporter != null)
            reporter.showProgress(level, maxLevel);
    }

    public DxfLAYER getLayer(String name, boolean refer)
    {
        return getTables().getLayer(name, refer);
    }

    public DxfLAYER getLayer(String name)
    {
        return getTables().getLayer(name, true);
    }

    public DxfLTYPE getLtype(String name)
    {
        return getTables().getLtype(name);
    }

    public DxfLTYPE[] getLtypes()
    {
        return getTables().getLtypes();
    }

    public DxfBLOCK getBlock(String name)
    {
        return blocks == null ? null : (DxfBLOCK)blocks.getBlock(name);
    }

    public DxfSTYLE getStyle(String name)
    {
        return getTables().getStyle(name);
    }

    public Hashtable getLayerTable()
    {
        return getTables().getLayerTable();
    }

    public int getLayerTableLength()
    {
        return getTables().getLayerTableLength();
    }

    public void convert(DxfConverter converter)
    {
        converter.convert(this);
    }

    public void convert(Dxf14Converter converter)
    {
        converter.convert(this);
    }

    public int getCodePage()
    {
        return header.getCodePage();
    }

    public DxfHeader getHeader()
    {
        return header;
    }

    public DxfTables getTables()
    {
        if(tables == null)
            tables = new DxfTables();
        return tables;
    }

    public DxfBlocks getBlocks()
    {
        return blocks;
    }

    public DxfEntities getEntities()
    {
        return entities;
    }

    static final int DEBUG_LEVEL = 0;
    protected DxfHeader header;             // Secao HEADER
    protected DxfTables tables;             // Secao TABLES
    protected DxfBlocks blocks;             // Secao BLOCKS
    protected DxfEntities entities;         // Secao ENTITIES
    protected DxfGroups grp;                // Grupos DXF
    protected DxfReporter reporter;         // Handler passado como parametro
    protected Hashtable countHash;          // Tabela Hash para armazenar ??????

    static 
    {
        I18n.addAppResourceBase("de.escape.quincunx.dxf.reader.DxfReaderResourceBundle");
    }
}